// 时间格式化工具
export const formatTime = (timeStr, format = 'YYYY-MM-DD HH:mm:ss') => {
  if (!timeStr) return '无数据'
  try {
    const date = new Date(timeStr)
    const year = date.getFullYear()
    const month = String(date.getMonth() + 1).padStart(2, '0')
    const day = String(date.getDate()).padStart(2, '0')
    const hours = String(date.getHours()).padStart(2, '0')
    const minutes = String(date.getMinutes()).padStart(2, '0')
    const seconds = String(date.getSeconds()).padStart(2, '0')

    return format
      .replace('YYYY', year)
      .replace('MM', month)
      .replace('DD', day)
      .replace('HH', hours)
      .replace('mm', minutes)
      .replace('ss', seconds)
  } catch (error) {
    return timeStr
  }
}

// 格式化 JSON 为易读格式
export const formatJSON = (jsonStr, space = 2) => {
  try {
    const obj = JSON.parse(jsonStr)
    return JSON.stringify(obj, null, space)
  } catch (error) {
    return jsonStr
  }
}

// 验证 JSON 格式是否合法
export const isJSON = (str) => {
  try {
    JSON.parse(str)
    return true
  } catch (error) {
    return false
  }
}